package com.yootk.main;

import com.yootk.event.YootkEvent;
import com.yootk.event.config.YootkEventConfig;
import com.yootk.vo.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Locale;

/**
 * ApplicationContext继承结构
 */
public class Demo07_ApplicationContext {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo07_ApplicationContext.class);

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    /**
     * 本地磁盘读取spring配置文件
     */
    public static void test1() {
        ApplicationContext context = new FileSystemXmlApplicationContext("F:\\muyan\\【test】spring-base.xml");
        Message message = context.getBean(Message.class);   // 获取指定类型的Bean
        LOGGER.info("获取message.path属性：{}", message.getPath());
    }

    /**
     * ApplicationContext继承的接口
     * EnvironmentCapable：实现资源的管理控制
     */
    public static void test2() {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-base.xml");
        ConfigurableEnvironment environment = context.getEnvironment();
        LOGGER.info("当前使用的JDK版本：{}", environment.getProperty("java.specification.version"));
    }

    /**
     * ApplicationContext继承的接口
     * ApplicationEventPublisher：事件发布器
     */
    public static void test3() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(YootkEventConfig.class);   // 注册配置类
        context.refresh();
        YootkEvent event = new YootkEvent("沐言科技：www.yootk.com");    // 设置事件类
        context.publishEvent(event);    // 事件发布
    }

    /**
     * ApplicationContext继承的接口
     * MessageSource：资源读取
     */
    public static void test4() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n.Message");
        LOGGER.info("【Message默认资源】yootk.site = {}", messageSource.getMessage("yootk.site", null, null));
        LOGGER.info("【Message中文资源】yootk.site = {}", messageSource.getMessage("yootk.site", null, new Locale("zh", "CN")));
        LOGGER.info("【Message英文资源】yootk.site = {}", messageSource.getMessage("yootk.site", null, new Locale("en", "US")));
    }
}
